package org.scratchcrew.domain.dao;

import java.util.HashMap;
import java.util.Map;

import org.scratchcrew.domain.ScratchData;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.stereotype.Repository;

@Repository("scratchDataDao")
public class ScratchDataDaoImpl extends EntityDaoSupport<ScratchData> implements
		ScratchDataDao {
	private static final String SCRATCH_ID_PARAM_NAME = "scratchId";
	private static final String FIND_BY_SCRATCH_ID_QUERY = "select sd"
			+ " from ScratchData sd where sd.scratch.id = :"
			+ SCRATCH_ID_PARAM_NAME;

	public ScratchDataDaoImpl() {
		super(ScratchData.class);
	}

	@Override
	public ScratchData save(ScratchData entity) {
		return getJpaTemplate().merge(entity);
	}

	@Override
	public ScratchData findByScratchId(Long scratchId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put(SCRATCH_ID_PARAM_NAME, scratchId);
		ScratchData result = (ScratchData) DataAccessUtils
				.uniqueResult(getJpaTemplate().findByNamedParams(
						FIND_BY_SCRATCH_ID_QUERY, parameters));
		return result;
	}

}
